import request from '@/utils/request'
import { AxiosPromise } from 'axios'

const getAllRoles = (): AxiosPromise<any> => {
  return request({
    method: 'GET',
    url: '/boss/role/all'
  })
}

interface SearchRolesType {
    size?: number
    current?: number
    name?: string
    startCreateTime?: string
    endCreateTime?: string
    id?: number
    code?: string
}

const searchRoles = (data: SearchRolesType = {}): AxiosPromise<any> => {
  return request({
    method: 'POST',
    url: '/boss/role/getRolePages',
    data
  })
}
interface CreateOrUpdateRoleType {
    id?: string
    name?: string
    code?: string
    description?: string
}

const createOrUpdateRole = (data: CreateOrUpdateRoleType): AxiosPromise<any> => {
  return request({
    method: 'POST',
    url: '/boss/role/saveOrUpdate',
    data
  })
}

const deleteRole = (id: string): AxiosPromise<any> => {
  return request({
    method: 'DELETE',
    url: `/boss/role/${id}`
  })
}

interface AllocateUserRolesType {
  userId: number|string
  roleIdList: number[]
}

const allocateUserRoles = (data: AllocateUserRolesType): AxiosPromise<any> => {
  return request({
    method: 'POST',
    url: '/boss/role/allocateUserRoles',
    data
  })
}

export {
  getAllRoles,
  searchRoles,
  createOrUpdateRole,
  deleteRole,
  allocateUserRoles
}
